<?php

// DataBase部分
function sendActive($uid, $content)
{
    $con = getConnection();
    $con->query("INSERT INTO `user_home_active` (`content`, `userid`) VALUES ('$content', '$uid');");
    return true;
}

function fetchActiveByUid($uid)
{
    $con = getConnection();
    $con->query("SELECT * FROM `user_home_active` WHERE `user_home_active`.`userid`='$uid'");
    return $con->fetch_all();
}

function fetchActiveByAid($aid)
{
    $con = getConnection();
    $con->query("SELECT * FROM `user_home_active` WHERE `user_home_active`.`aid`='$aid'");
    return $con->fetch_assoc();
}

function updateActiveHasComment($aid, $state)
{
    $con = getConnection();
    $con->query("UPDATE `user_home_active` SET `has_comment` = '$state' WHERE `user_home_active`.`aid` = $aid;");
    return true;
}

function fetchActiveHasCommentFromCommentTable($aid)
{
    $con = getConnection();
    $con->query("SELECT paid FROM `user_home_comment` WHERE `paid` = $aid;");
    return $con->getRows() != 0;
}

function removeActive($aid)
{
    $con = getConnection();
    $data = fetchActiveByAid($aid);
    if (empty($data)) {
        return false;
    }
    $uid = $data["userid"];
    $con->query("DELETE FROM `user_home_active` WHERE `user_home_active`.`aid` = '$aid';");
    return true;
}

function sendComment($paid, $pcid, $content, $uid)
{
    $con = getConnection();
    $con->query("INSERT INTO `user_home_comment` (`content`, `paid`, `pcid`, `uid`) VALUES ( '$content', '$paid', '$pcid', '$uid');");
    updateActiveHasComment($paid, "1");
    return true;
}

function fetchCommentByAid($aid)
{
    $con = getConnection();
    $con->query("SELECT * FROM `user_home_comment` WHERE `user_home_comment`.`paid`='$aid'");
    return $con->fetch_all();
}

function fetchPaidByCid($cid)
{
    $con = getConnection();
    $con->query("SELECT paid FROM `user_home_comment` WHERE `user_home_comment`.`cid`='$cid'");
    return $con->fetch_assoc()["paid"];
}

function removeCommentByAid($aid)
{
    updateActiveHasComment($aid, "0");
    $con = getConnection();
    $con->query("DELETE FROM `user_home_comment` WHERE `user_home_comment`.`paid` = '$aid';");
    return true;
}

function removeCommentByCid($cid)
{
    $paid = fetchPaidByCid($cid);
    if ($paid == "") {
        return false;
    }
    $con = getConnection();
    $con->query("DELETE FROM `user_home_comment` WHERE `user_home_comment`.`cid`='$cid' OR `user_home_comment`.`pcid`='$cid';");
    if (! fetchActiveHasCommentFromCommentTable($paid)) {
        updateActiveHasComment($paid, "0");
    }
    return true;
}

function sendLeaveMessage($uid, $target, $content)
{
    $con = getConnection();
    $con->query("INSERT INTO `user_home_leavemessage` (`author`, `target`, `content`) VALUES ('$uid', '$target', '$content');");
    return true;
}

function removeMessage($lid)
{
    $con = getConnection();
    $data = fetchLeaveMessage($lid);
    if (empty($data)) {
        return false;
    }
    $uid = $data["target"];
    $con->query("DELETE FROM `user_home_leavemessage` WHERE `user_home_leavemessage`.`lid` = '$lid';");
    return true;
}

function fetchLeaveMessage($lid)
{
    $con = getConnection();
    $con->query("SELECT * FROM `user_home_leavemessage` WHERE `user_home_leavemessage`.`lid`='$lid'");
    return $con->fetch_assoc();
}

function fetchLeaveMessageByTarget($target)
{
    $con = getConnection();
    $con->query("SELECT * FROM `user_home_leavemessage` WHERE `user_home_leavemessage`.`target`='$target'");
    return $con->fetch_assoc();
}

function fetchLeaveMessageByAuthor($author)
{
    $con = getConnection();
    $con->query("SELECT * FROM `user_home_leavemessage` WHERE `user_home_leavemessage`.`target`='$author'");
    return $con->fetch_assoc();
}

function replyMessage($lid, $content, $uid)
{
    $data = fetchLeaveMessage($lid);
    if (empty($data)) {
        return false;
    }
    $target = $data["target"];
    $con = getConnection();
    $con->query("INSERT INTO `user_home_leavemessage` (`author`, `target`, `is_reply`, `content`) VALUES ('$uid', '$target', '1', '$content');");
    return true;
}

function getLeaveMessageAmount($target)
{
    $con = getConnection();
    $con->query("SELECT COUNT(*) FROM `user_home_leavemessage` WHERE `target` = $target");
    return $con->fetch_assoc()["COUNT(*)"];
}

function getActiveAmount($uid)
{
    $con = getConnection();
    $con->query("SELECT COUNT(*) FROM `user_home_active` WHERE `userid` = $uid");
    return $con->fetch_assoc()["COUNT(*)"];
}

function getLeaveMessageToAmount($author)
{
    $con = getConnection();
    $con->query("SELECT COUNT(*) FROM `user_home_leavemessage` WHERE `author` = $author");
    return $con->fetch_assoc()["COUNT(*)"];
}

function fetchActiveByPage($uid, $page)
{
    global $_GCONFIG;
    $size = $_GCONFIG["home"]["active"]["pagesize"];
    $con = getConnection();
    $amount = getPageAmount(getActiveAmount($uid), $size);
    if ($page > $amount) {
        return array();
    }
    $lim1 = getLimit($page, 10)[0];
    $lim2 = getLimit($page, 10)[1];
    $con->query("SELECT * FROM `user_home_active` WHERE `userid` = $uid ORDER BY `time` DESC LIMIT $lim1,$lim2 ");
    return $con->fetch_all();
}

function fetchLeaveMessageToByPage($author, $page)
{
    $con = getConnection();
    $amount = getPageAmount(getLeaveMessageToAmount($author), 10);
    if ($page > $amount) {
        return array();
    }
    $lim1 = getLimit($page, 10)[0];
    $lim2 = getLimit($page, 10)[1];
    $con->query("SELECT * FROM `user_home_leavemessage` WHERE `author` = $author ORDER BY `time` DESC LIMIT $lim1,$lim2 ");
    return $con->fetch_all();
}

function fetchLeaveMessageByPage($target, $page)
{
    $con = getConnection();
    $amount = getPageAmount(getLeaveMessageAmount($target), 10);
    if ($page > $amount) {
        return array();
    }
    $lim1 = getLimit($page, 10)[0];
    $lim2 = getLimit($page, 10)[1];
    $con->query("SELECT * FROM `user_home_leavemessage` WHERE `target` = $target ORDER BY `time` DESC LIMIT $lim1,$lim2 ");
    return $con->fetch_all();
}

// 逻辑处理部分
function getPageAmount($size, $pageSize)
{
    return ceil($size / $pageSize);
}

function getLimit($page, $pageSize)
{
    return array(
        ($page - 1) * $pageSize,
        $pageSize
    );
}

function getActiveMaxPage($uid)
{
    global $_GCONFIG;
    $size = $_GCONFIG["home"]["active"]["pagesize"];
    $amo = getActiveAmount($uid);
    if ($amo == 0) {
        return - 1;
    }
    return getPageAmount($amo, $size);
}
?>